package com.itbaima.controller.HRMM;


import com.itbaima.pojo.VO.HrArchiveVO;
import com.itbaima.pojo.entity.HrArchive;
import com.itbaima.result.Result;
import com.itbaima.service.HumanService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/human")
public class UserController {

    @Autowired
    private HumanService humanService;


    @PostMapping
    public Result save(@RequestBody HrArchive hrArchive , HttpSession session){


        Integer userId = (Integer) session.getAttribute("userId");
        String name = (String) session.getAttribute("name");

        if(userId == null || name == null){
            return Result.error("当前用户未登录，无法录入档案信息");
        }

        log.info("开始录入档案信息：{} 由 {}（ID: {}）登记", hrArchive, name, userId);

        humanService.save(hrArchive,userId,name);

        return Result.success("档案录入信息成功");
    }


    @GetMapping
    public Result<List<HrArchiveVO>> getAll(){
        log.info("查询所有人力资源档案中：");
        List<HrArchiveVO> hrArchiveList = humanService.getAll();
        return Result.success(hrArchiveList);
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("开始删除对应ID的人力资源档案：{}", id);
        humanService.delete(id);
        return Result.success();
    }

    @PutMapping
    public Result update(@RequestBody HrArchiveVO hrArchiveVO , HttpSession session){

        Integer userId = (Integer) session.getAttribute("userId");
        String name = (String) session.getAttribute("name");

        if(userId == null || name == null){
            return Result.error("当前用户未登录，无法录入档案信息");
        }

        log.info("开始修改人力资源档案：{} , 修改人为{}（ID：{}）" , hrArchiveVO , name , userId);

        humanService.update(hrArchiveVO,name,userId);


        return Result.success();
    }






//    @GetMapping("/page")
//    public Result<List<HrArchive>> dataQuery(){
//
//    }


}
